import 'package:animal/constant/color.dart';
import 'package:animal/constant/font.dart';
import 'package:animal/utils/screen_util.dart';
import 'package:animal/widgets/HeaderWidget.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:getwidget/getwidget.dart';

///购物车item
class ShopCartItem extends StatefulWidget {
  final TextEditingController? controller;
  final void Function(bool)? onChanged;
  final String? norms; //规格
  final String? price; //价格

  const ShopCartItem({Key? key, this.controller, this.onChanged, this.norms, this.price}) : super(key: key);

  @override
  State<ShopCartItem> createState() => _ShopCartItemState();
}

class _ShopCartItemState extends State<ShopCartItem> {
  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.only(top: ScreenUtil().L(15)),
      child: Row(
        children: [
          GFCheckbox(
            onChanged: widget.onChanged,
            value: false,
            type: GFCheckboxType.circle,
            inactiveBorderColor: KColorConstant.black999999,
            inactiveBgColor: Colors.white,
            size: ScreenUtil().L(20),
          ),
          Container(width: ScreenUtil().L(15)),
          Header.defaultHead(width: 80, height: 80),
          Container(width: ScreenUtil().L(15)),
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  "挺野钓箱全套2019新款渔具多功能钓鱼箱2020款特价箱超轻箱子装备...",
                  style: KFontConstant.myTextStyle(size: 14, color: const Color(0XFF3C3D40)),
                  maxLines: 2,
                  overflow: TextOverflow.ellipsis,
                ),
                Container(
                    alignment: Alignment.center,
                    width: ScreenUtil().L(100),
                    margin: EdgeInsets.symmetric(vertical: ScreenUtil().L(10)),
                    padding: EdgeInsets.symmetric(horizontal: ScreenUtil().L(5), vertical: ScreenUtil().L(4)),
                    decoration: BoxDecoration(color: const Color(0XFFFFEDED), borderRadius: BorderRadius.circular(ScreenUtil().L(30))),
                    child: Text(
                      widget.norms ?? "规格名称DGD",
                      style: KFontConstant.myTextStyle(size: 10, color: const Color(0XFFA9A9A9)),
                      overflow: TextOverflow.ellipsis,
                    )),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Text("¥${widget.price ?? 0}", style: KFontConstant.myTextStyle(size: 14, color: Colors.red, bold: true)),
                    Row(
                      children: [
                        GFIconButton(
                          icon: const Icon(
                            Icons.remove,
                            color: Colors.black,
                          ),
                          size: GFSize.SMALL,
                          onPressed: () => numReduce(),
                          type: GFButtonType.transparent,
                        ),
                        Container(
                          width: ScreenUtil().L(30),
                          height: ScreenUtil().L(23),
                          alignment: Alignment.center,
                          decoration: BoxDecoration(color: KColorConstant.textFieldBgColorFFEDED, borderRadius: BorderRadius.circular(ScreenUtil().L(2))),
                          child: TextField(
                            controller: widget.controller!,
                            textAlign: TextAlign.center,
                            style: KFontConstant.myTextStyle(size: 14),
                            decoration: const InputDecoration(
                              border: InputBorder.none,
                              isCollapsed: true,
                            ),
                            keyboardType: TextInputType.number,
                            inputFormatters: [FilteringTextInputFormatter.digitsOnly], //只允许输入数字
                          ),
                        ),
                        GFIconButton(
                          icon: const Icon(
                            Icons.add,
                            color: Colors.black,
                          ),
                          size: GFSize.SMALL,
                          onPressed: () => numAdd(),
                          type: GFButtonType.transparent,
                        ),
                      ],
                    )
                  ],
                )
              ],
            ),
          ),
        ],
      ),
    );
  }

  ///数量增加的方法
  void numAdd() {
    int a = int.parse(widget.controller!.text.trim());
    setState(() {
      ++a;
      widget.controller!.text = a.toString();
    });
  }

  ///数量增加的方法
  void numReduce() {
    int a = int.parse(widget.controller!.text.trim());
    if(a>0){
      setState(() {
        --a;
        widget.controller!.text = a.toString();
      });
    }
  }
}
